function getSiteCfg() { 
    $.getJSON(
        "/api/tmpo/config_public/",
        function(json){
            var val = 'POC: <a class="POCemail" href="mailto:' +
                      json.tmpo_poc_email + '">' +
                      json.tmpo_poc_name + '</a> ' +
                      'Phone: (' + json.tmpo_poc_phone.substr(0,3) + ') ' +
                      json.tmpo_poc_phone.substr(3,3) + '-' + 
                      json.tmpo_poc_phone.substr(6,4);
            $("span.POCemail").append(val); 
        }
    ); 
}


defaultPageFieldSet = function () {return $("fieldset[o_type=defaultPath]");};
optCookieName = function () {return defaultPageFieldSet().attr('o_name');};
pageMap = {};

onSaveOptions = function() {
    var option = $("input[type=radio]:checked", defaultPageFieldSet()).val();
    if (option && option in pageMap) {
        $.cookie(optCookieName(), pageMap[option], {expires:1024, path:"/"});
        $(".mainContent h2").text("Option Saved");
        $(".mainContent span:first").text(
            "Select a different option or continue to another page.");
        $("img.headerLogo").parent().attr('href', pageMap[option]);
    }
    return false;
};

onCancelOptions = function() {
    window.history.back();
    return false;
};

$(function() {
    // build the key/value pairs on the pageMap object from the radio inputs
    Functional.install();
    getSiteCfg();
    map(function (i) {pageMap[i[0]] = i[1];},
        map(function (f){return [f.value, $(f).attr('o_path')];}, 
            $("input[type=radio]", defaultPageFieldSet())));
    var usr_def = $.cookie(optCookieName());
    var which = filter(function (item){
            if(usr_def==item[1]){return true;}
        }, items(pageMap));
    if (which && which[0]) {
        $("#form1 input[value=" + which[0][0] + "]").attr("checked", "true");
    }
    $("#optSave").click(onSaveOptions);
    $("#optCancel").click(onCancelOptions);
});
